Component({
  /**
   * 组件的属性列表
   */
  properties: {
    title: {
      type: String,
      value: '标题'
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    //弹窗显示控制 
    showModalStatus: false,
  },
  /**
   * 组件的方法列表
   */
  methods: {
    //点击显示底部弹出
    changeRange: function () {
      return new Promise<void>((resolve, reject) => {
        try {
          this.showModal();
          resolve();
        } catch (error) {
          reject(error);
        }
      });
    }
    ,

    //底部弹出框
    showModal: function () {
      // 背景遮罩层
      var animation = wx.createAnimation({
        duration: 200,
        timingFunction: "linear",
        delay: 0
      })
      //this.animation = animation
      animation.translateY(300).step()
      this.setData({
        animationData: animation.export(),
        showModalStatus: true
      })
      setTimeout(function () {
        animation.translateY(0).step()
        this.setData({
          animationData: animation.export()
        })
      }.bind(this), 200)
    },
    goYun: function () {
      // 要跳转的页面路径
      var targetPagePath = '../yun/yun';
      this.setData({
        currentIndex: 1
      })
      // 使用wx.navigateTo方法跳转到指定页面
      wx.redirectTo({
        url: targetPagePath,
        disableScroll: true
      });
    },
    //点击背景面任意一处时，弹出框隐藏
    hideModal: function () {
      //弹出框消失动画
      var animation = wx.createAnimation({
        duration: 200,
        timingFunction: "linear",
        delay: 0
      })
      //this.animation = animation
      animation.translateY(300).step()
      this.setData({
        animationData: animation.export(),
      })
      setTimeout(function () {
        animation.translateY(0).step()
        this.setData({
          animationData: animation.export(),
          showModalStatus: false
        })
      }.bind(this), 200)
    },
    async handlewx() {
      try {
        const res = await new Promise((resolve, reject) => {
          wx.chooseMessageFile({
            count: 10,
            type: 'all',
            success: resolve,
            fail: reject
          });
        });
    
        const tempFilePaths = res.tempFiles;
        console.log(tempFilePaths);
    
        // Store file names in an array
        const fileNames = tempFilePaths.map(file => file.name);
        wx.setStorageSync('fileNames', fileNames);
    
        for (const file of tempFilePaths) {
          try {
            const uploadRes = await new Promise((resolve, reject) => {
              wx.uploadFile({
                url: 'http://server-url.com/upload', 
                filePath: file.path,
                name: 'file',
                formData: {
                  'file': file.path
                },
                header: {
                  [wx.getStorageSync('tokenName')]: wx.getStorageSync('token'),
                },
                success: resolve,
                fail: reject
              });
            });
    
            const data = JSON.parse(uploadRes.data);
            console.log(data);
            if (data.code === 200) {
              wx.showToast({
                title: '上传成功',
                icon: 'none',
                duration: 1300
              });
            } else {
              wx.showToast({
                title: data.message || '上传失败',
                icon: 'none',
                duration: 2000
              });
            }
          } catch (err) {
            console.error('Upload failed:', err);
            wx.showToast({
              title: '上传失败',
              icon: 'none',
              duration: 2000
            });
          }
        }
      } catch (err) {
        console.error('File selection failed:', err);
        wx.showToast({
          title: '文件选择失败',
          icon: 'none',
          duration: 2000
        });
      }
    },
    
    handleBd: function () {
      wx.chooseMedia({
        count: 10,
        mediaType: ["image"],
        success(res) {
          const tempFilePaths = res.tempFiles
          console.log(tempFilePaths)
          for (var i in tempFilePaths) {
            wx.uploadFile({
              url: 'http://baidu.com', //上传的服务器地址
              filePath: tempFilePaths[i].tempFilePath,
              name: 'file',
              formData: {
                'file': tempFilePaths[i].tempFilePath
              },

              header: {
                [wx.getStorageSync('tokenName')]: wx.getStorageSync('token'),
              },
              success: function (resp) {
                console.log(resp)
                var data = JSON.parse(resp.data)
                console.log(data)
                if (data.code == 200) {
                  wx.showToast({
                    title: '上传成功',
                    icon: 'none',
                    duration: 1300
                  })
                } else {
                  wx.showToast({
                    title: data.message,
                    icon: 'none',
                    duration: 2000
                  })
                }
              },
              fail: function (err) {
                console.log(err)
              }
            })
          }
        }
      })

    },
  }
})
